package com.lanlian.cayman.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqFriendAuth;
import com.lanlian.commonlib.bean.RespFriendAuth;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.uikit.MyImageView;
import com.lanlian.utillib.DisplayUtil;
import com.lanlian.utillib.ImageUtils;
import com.lanlian.utillib.QRCodeUtil;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/7 17:54
 *     desc   : 好友认证
 *     version: 1.0
 * </pre>
 */
public class FriendAuthenticationActivity extends BaseActivity implements View.OnClickListener {

    private MyImageView mImgQrcode;
    private LinearLayout mLaySave;
    private LinearLayout mLayCopy;
    private LinearLayout mLayFriend;
    private TextView mTvSurname1;
    private TextView mTvName1;
    private TextView mTvSurname2;
    private TextView mTvName2;
    private TextView mTvSurname3;
    private TextView mTvName3;
    private TextView mTvSurname4;
    private TextView mTvName4;
    private TextView mTvSurname5;
    private TextView mTvName5;

    private TextView[] mTvSurnames;
    private TextView[] mTvNames;

    private Bitmap mBitmap;
    private String mMobile;
    private RespFriendAuth mRespFriendAuth;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_friend, true);
        initView();
        initData();
        setListeners();
        getFriendAuth();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("好友认证");
        mImgQrcode = findViewById(R.id.img_qrcode);
        mLaySave = findViewById(R.id.lay_save);
        mLayCopy = findViewById(R.id.lay_copy);
        mLayFriend = findViewById(R.id.lay_friend);
        mTvSurname1 = findViewById(R.id.tv_surname1);
        mTvName1 = findViewById(R.id.tv_name1);
        mTvSurname2 = findViewById(R.id.tv_surname2);
        mTvName2 = findViewById(R.id.tv_name2);
        mTvSurname3 = findViewById(R.id.tv_surname3);
        mTvName3 = findViewById(R.id.tv_name3);
        mTvSurname4 = findViewById(R.id.tv_surname4);
        mTvName4 = findViewById(R.id.tv_name4);
        mTvSurname5 = findViewById(R.id.tv_surname5);
        mTvName5 = findViewById(R.id.tv_name5);
        mTvSurnames = new TextView[]{mTvSurname1, mTvSurname2, mTvSurname3, mTvSurname4, mTvSurname5};
        mTvNames = new TextView[]{mTvName1, mTvName2, mTvName3, mTvName4, mTvName5};
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mLayCopy.setOnClickListener(this);
        mLaySave.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        mMobile = getIntent().getStringExtra("mobile");
    }

    private void getFriendAuth() {
        showLoadingDialog();
        VolleyRequestUtils.getInstance().sendRequest(this.toString(), new ReqFriendAuth(mMobile),
                new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        dismissLoadingDialog();
                        mRespFriendAuth = (RespFriendAuth) response;
                        setFriendAuth();
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        dismissLoadingDialog();
                        showToast(baseResponse.getMsg());
                    }
                });
    }

    private void setFriendAuth() {
        mBitmap = QRCodeUtil.createQRCodeBitmap(mRespFriendAuth.getUrl() + "?num=" + mMobile, DisplayUtil.dip2px(this, 200),
                DisplayUtil.dip2px(this, 200), true);
        mImgQrcode.setImageBitmap(mBitmap);
        int size = mRespFriendAuth.getDataList().size() < 5 ? mRespFriendAuth.getDataList().size() : 5;
        for (int i = 0; i < size; i++) {
            mTvSurnames[i].setVisibility(View.VISIBLE);
            mTvNames[i].setVisibility(View.VISIBLE);
            String friend = mRespFriendAuth.getDataList().get(i).getFriendName();
            mTvSurnames[i].setText(friend.substring(0, 1));
            mTvNames[i].setText(friend);
        }
        if (mRespFriendAuth.getDataList().size() == 0) {
            mLayFriend.setVisibility(View.GONE);
        } else {
            mLayFriend.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_copy:
                if (mRespFriendAuth != null) {
                    ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData clipData = ClipData.newPlainText("text", mRespFriendAuth.getUrl() + "?num=" + mMobile);
                    cm.setPrimaryClip(clipData);
                    showToast("复制成功");
                }
                break;
            case R.id.lay_save:
                if (mBitmap != null) {
                    String string = ImageUtils.saveBitmap(this, mBitmap, String.format("%s_%s.jpg", System.currentTimeMillis(), "胡晓波"));
                    if (TextUtils.isEmpty(string)) {
                        showToast("保存失败");
                    } else {
                        showToast("保存成功");
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBitmap != null && !mBitmap.isRecycled()) {
            mBitmap.recycle();
            mBitmap = null;
        }
    }
}
